{include file="public/head"/}
<link rel="stylesheet" href="__PUBLIC__phone/css/account.css"/>
<body>
<!--头部-->
<header id="headers">
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <a href="javascript:;" class="leftRuted glyphicon glyphicon-menu-left pull-left"></a>
                <p>我的账户</p>
            </div>
        </div>
    </div>
</header>
<!-- 我的账户-充值 -->
<div class="account normalBox">
    <div class="container countwrap">
        <ul class="payul">
            <li>
                当前余额：<span>￥{$balance}元</span>
            </li>
            <li>
                充值金额：
                <input type="text" style="text-align: center;" name="money" value="1" class="input-sm"/>
                <span class="mins">*充值金额为正整数，最小单位1元</span>
            </li>
            <li>
                充值方式：
                {if condition="$ifAuth eq true"}
                <span class="btn btn-default active">微信</span>
                {else/}
                <span class="btn btn-default" onclick="javascript:reminder('暂时不支持微信在线充值！');" style="background: #ccc;">微信</span>
                {/if}
            </li>
            <li>
                <a href="javascript:;" class="payaBtn">充值</a>
            </li>
        </ul>
    </div>
</div>
<script>
    $(function(){
        var rInput = $('input[name="money"]');
        rInput.keydown(function(event){
            if(event.which == '40' || event.which == '38'){   // 禁止方向键点击
                return false;
            }
        }).keyup(function(){   // 替换非数字为空
            var upValue = $(this).val();
            $(this).val(upValue.replace(/[^\d]/g, ''));
        }).blur(function(){   // 数据为空，默认最小数据
            if(!$(this).val()){
                $(this).val(1);
            }
        })

        // JSAPI支付
        var ifAuth = "<?php echo $ifAuth; ?>";
        $('.payaBtn').on('click',function(){
            if(!ifAuth){
                reminder('暂时不支持微信支付！');
                return false;
            }
            var rMoney = rInput.val();
            $.post("{:url('home/Personal/wxPay')}",{'money':rMoney},function(data){
                if(data.code == '0'){
                    callpay(data.msg);
                } else {
                    reminder(data.msg);
                }
            })
        })
    })

    function callpay(data)
    {
        if (typeof(WeixinJSBridge) == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall(data);
        }
    }

    //调用微信JS api 支付
    function jsApiCall(data)
    {
        WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                {
                    "appId":data.appId,     //公众号名称，由商户传入
                    "timeStamp":data.timeStamp,         //时间戳，自1970年以来的秒数
                    "nonceStr":data.nonceStr, //随机串
                    "package":data.package,
                    "signType":"MD5",         //微信签名方式：
                    "paySign":data.paySign //微信签名
                },
                function(res){
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        location.href = "http://"+window.location.host+"/home/Index/index";
                    } else {
                        if (res.err_msg == "get_brand_wcpay_request:cancel") {
                            reminder('你取消了微信支付');
                        } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                            reminder('微信支付失败');
                        }
                    }
                }
        );
    }
</script>
</body>
</html>